package com.itheima.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.sky.dao.CategoryMapper;
import com.itheima.sky.dao.DishMapper;
import com.itheima.sky.mapper.DishFlavorMapper;
import com.itheima.sky.dto.DishDto;
import com.itheima.sky.entity.PageResult;
import com.itheima.sky.entity.Result;
import com.itheima.sky.pojo.Category;
import com.itheima.sky.pojo.Dish;
import com.itheima.sky.pojo.DishFlavor;
import com.itheima.sky.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description DishControllerImpl
 * @Author jiangning
 * @Date 2024-06-29
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Autowired(required = false)
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    CategoryMapper categoryMapper;


    @Override
    public List<Dish> getDishByCategoryId(BigDecimal cateId,Integer status) {
        return dishMapper.getDishListByCategoryId(cateId,status);
    }

    //p


    @Override
    public Result page(int page, int pageSize, String name) {
        //1. 设置当前页与页面大小
        PageHelper.startPage(page,pageSize);
        //2. 查找页面的数据
        List<Dish> dishList =  dishMapper.findByNameP(name);

        //3.构建pageInfo对象
        PageInfo<Dish> pageInfo = new PageInfo<>(dishList);
        List<DishDto>  dishDtoList = new ArrayList<>();
//        List<DishDto> collect = pageInfo.getList().stream().map((dish -> {
//            DishDto dishDto = new DishDto();
//            BeanUtils.copyProperties(dish, dishDto);
//            Category category = categoryMapper.findById(dish.getCategoryId());
//            dishDto.setCategoryName(category.getName());
//            return dishDto;
//        })).collect(Collectors.toList());
        List<DishDto> collect = new ArrayList<>();
        for (Dish dish : pageInfo.getList()) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Category category = categoryMapper.findById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            collect.add(dishDto);
        }



        return Result.success(new PageResult(pageInfo.getTotal(),collect));
    }



    /**
     * 作用：根据id查找菜品和菜品的口味
     * @param id  菜品的id
     * @return
     */
    @Override
    public DishDto findById(Long id) {
        //1.根据id查找菜品
        Dish dish = dishMapper.findById(id);

        //2. 根据菜品的id查找菜品对应的口味
        List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(id);

        //3. 创建DishDto，把菜品、口味信息封装到DishDto里面
        DishDto dishDto = new DishDto();
        //属性拷贝
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }


    /**
     * 作用：修改菜品
     * @param dishDto 页面传递过来的参数包含菜品与口味
     * @return
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //1. 补全修改时间
        dishDto.setUpdateTime(LocalDateTime.now());
//        dishDto.setUpdateUser(1L);
        //2 修改菜品
        dishMapper.updateByIdP(dishDto);

        //3. 删除该菜品的所有口味信息
        dishFlavorMapper.deleteByDishId(dishDto.getId());

        //4. 补全口味信息，然后重新插入
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> dishFlavorList = flavors.stream().map((flavor -> {
            //对每一个元素进行加工
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            return flavor;
        })).collect(Collectors.toList());

        //4. 批量保存口味信息
        dishFlavorMapper.saveBatch(dishFlavorList);

    }


    /**
     * 作用：保存菜品并且携带口味信息
     * @param dishDto  用于接收菜品与口味信息
     * @return
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //1. 给菜品补全创建时间，修改时间,sort
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDto.setStatus(1);
        //2. 保存菜品
        dishMapper.save(dishDto);  //学习mybatis的时候，保存数据可以让主键列设置给当前的实体类对象

        //3. 补全口味的 dish_id 、 创建时间、修改时间、创建人、修改人、
        //获取所有的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历所有口味信息，补全dish_id 、 创建时间、修改时间、创建人、修改人、
        //使用jdk8的新特性，stream, map映射
        List<DishFlavor> dishFlavorList = flavors.stream().map((flavor -> {
            //对每一个元素进行加工
            flavor.setDishId(dishDto.getId());
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setUpdateTime(LocalDateTime.now());
            flavor.setCreateUser(dishDto.getCreateUser());
            flavor.setUpdateUser(dishDto.getUpdateUser());
            return flavor;
        })).collect(Collectors.toList());

        //4. 批量保存口味信息
        dishFlavorMapper.saveBatch(dishFlavorList);
    }

    //o
    public DishServiceImpl(DishMapper dishMapper) {
        this.dishMapper = dishMapper;
    }

    /**
     * 根据菜品类别的id查找的菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> findByCategoryId(Long categoryId, String name) {

        //查找菜品id查询菜品
        List<Dish> dishList = dishMapper.findDishAll(categoryId,name);

        return dishList;
    }
}
